home *** CD-ROM | disk | FTP | other *** search
/ 1,000+ Great Games / 1_1000 Games.iso / GAMES_1 / CDNTIMER.ZIP / CDNTIMER.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-01  |  7.6 KB  |  305 lines

  1. /****************************** BOARD GAME TIMER****************************
  2. Full-featured "turn timer"      Digital displays of time
  3. for board games.  
  4. Also usable as cooking                            
  5. darkroom timer, etc.                 
  6.                 Low-time warning (optional)
  7.                 Flashing "tick" indicator (optional)
  8. ****************************************************************************/
  9.  
  10.  
  11. #include <stdio.h>
  12. #include <time.h>
  13. #include <conio.h>
  14. #include <stdlib.h>
  15. #include <graphics.h>
  16. #include <dos.h>
  17. #include "oscr.hpp"
  18. #include "bt.hpp"
  19.  
  20. void main()
  21. {
  22.    randomize();
  23.    opening_screen();
  24.    play();
  25. }
  26.  
  27. void CountdownTimer::clock_on()
  28. {
  29.    time_t prev_sec;
  30.    int ch;
  31.  
  32.       text_color = LIGHTRED;
  33.  
  34.       settextstyle( TRIPLEX_FONT, HORIZ_DIR, 1 );
  35.       settextjustify( LEFT_TEXT, TOP_TEXT );
  36.  
  37.       startn_t = time ( NULL );  //Click on stopwatch.
  38.       setcolor( BKGRND_COLOR );
  39.       outtextxy( NAME_POS, 100, "Press a key to reset timer" );
  40.       setcolor( LIGHTBLUE );
  41.       outtextxy( NAME_POS, 100, "Press a key to stop timer" );
  42.  
  43.  
  44.       settextstyle( TRIPLEX_FONT, HORIZ_DIR, 5 );
  45.       display_time();  //Otherwise initial time not displayed...
  46.  
  47.       while( !( ch = kbhit() ) )
  48.      {
  49.      prev_sec = seconds;
  50.      interval_t = time( NULL ) - start_t;
  51.      running_t = total_seconds - interval_t;
  52.      convert( running_t );
  53.  
  54.      if( seconds - prev_sec )
  55.         {
  56.         display_time();
  57.  
  58.         if( !seconds )
  59.            if( minutes == warning )
  60.           if( !hours )
  61.              if( time_warning_flag )
  62.             blatt();
  63.  
  64.         if( visual_ticking_flag ) // Show blinking box ticks?
  65.            {
  66.            setfillstyle( random ( PATTERNS ), random ( COLORS ) );
  67.            setcolor ( random ( COLORS ) );
  68.            setlinestyle( SOLID_LINE, 0xFFF, NORM_WIDTH );
  69.            bar( X_C - RADIUS, Y_C - RADIUS,
  70.             X_C + RADIUS, Y_C + RADIUS );
  71.            }
  72.         }
  73.  
  74.      if( timeout() )
  75.         {
  76.         exit_();
  77.         reset_timer();
  78.  
  79.         settextstyle( TRIPLEX_FONT, HORIZ_DIR, 5 );
  80.         display_time();
  81.         return;  /*Move counter increments*/
  82.         }
  83.  
  84.      }
  85.  
  86.       ch = getch();
  87.       if( ch == ESC )
  88.      exit__();  // Quit.
  89.  
  90.       running_flag = OFF; // Reset each time.
  91.       setcolor( BKGRND_COLOR );
  92.       settextstyle( TRIPLEX_FONT, HORIZ_DIR, 1 );
  93.       outtextxy( NAME_POS, 100, "Press a key to stop timer" );
  94.       setcolor( CYAN );
  95.       outtextxy( NAME_POS, 100, "Press a key to reset timer" );
  96.       ch = getch();
  97.       if( ch == ESC )
  98.      exit__(); // Quit.
  99.  
  100.       reset_timer(); 
  101.  
  102.       return;
  103.  
  104. }
  105.  
  106. void CountdownTimer::display_moves()
  107. {
  108.    char buf[ 5 ];
  109.    static char ebuf[ 5 ];
  110.  
  111.       if( moves > 1 )
  112.      {
  113.      setcolor( WHITE );
  114.      settextstyle( TRIPLEX_FONT, HORIZ_DIR, 1 );
  115.      settextjustify( CENTER_TEXT, TOP_TEXT );
  116.      outtextxy( MOVES_X, MOVES_Y, ebuf );
  117.      }
  118.  
  119.       sprintf( buf, "%003d", moves );
  120.       sprintf( ebuf, buf );
  121.       setcolor( GREEN );
  122.       settextstyle( TRIPLEX_FONT, HORIZ_DIR, 1 );
  123.       settextjustify( CENTER_TEXT, TOP_TEXT );
  124.       outtextxy( MOVES_X, MOVES_Y, buf );
  125.       
  126.       return;
  127. }
  128.  
  129. void graphics_setup( int background_color )
  130. {
  131.    int grdriver = VGA,
  132.        grmode = VGAHI;
  133.  
  134.        registerfarbgidriver( EGAVGA_driver_far );
  135.        registerfarbgifont( gothic_font_far );
  136.        registerfarbgifont( triplex_font_far );
  137.        initgraph( &grdriver, &grmode, "" );
  138.        setbkcolor( background_color );
  139.  
  140. }
  141.  
  142. void exit__()
  143. {
  144.       closegraph();
  145.       exit( QUIT );
  146. }
  147.  
  148.     /***************Routine to erase old numbers*************/
  149. void CountdownTimer::erase_numbers()
  150. {
  151.       setcolor ( WHITE ); 
  152.  
  153.       if( seconds == 59 )
  154.      outtextxy( BLK_TIME, Y_TIMEPOS, line_clear );
  155.       else
  156.      if( seconds == 9 || seconds == 19 || seconds == 29
  157.          || seconds == 39 || seconds == 49 )
  158.         outtextxy( BLK_TIME + POS1_OFFSET, Y_TIMEPOS,
  159.                line_clear + 6 );
  160.       else
  161.          outtextxy( BLK_TIME + POS_OFFSET, Y_TIMEPOS,
  162.             line_clear + 7 ); 
  163.  
  164.      return;
  165.  
  166. }
  167.  
  168.  
  169. void play()
  170. {
  171.    int hrs,
  172.        min;
  173.    char inputstr[ MAXLEN ],
  174.     inp;
  175.  
  176.       clrscr();
  177.  
  178.       textcolor ( RED );
  179.       cprintf( "\n                                   MINUTES: " );
  180.       gets( inputstr );
  181.       min = atoi( inputstr );
  182.       CountdownTimer t1( min );
  183.  
  184.       textcolor( CYAN );
  185.       cprintf( "\n\n                         Enable flashing clock ticks? " );
  186.       inp = getche();
  187.       if( inp == 'y' || inp == 'Y' )
  188.      t1.visual_ticking_flag = ON;
  189.       else
  190.      t1.visual_ticking_flag = OFF;
  191.  
  192.       cprintf( "\n                                                      Enable time warning? " );
  193.       inp = getche();
  194.       if( inp == 'y' || inp == 'Y' )
  195.      {
  196.      t1.time_warning_flag =  ON;
  197.      cprintf( "                                                          At how many minutes? " );
  198.      gets( inputstr );
  199.      t1.warning = atoi( inputstr );
  200.      }
  201.       else
  202.      t1.time_warning_flag = OFF;
  203.  
  204.       textcolor( YELLOW | BLINK );
  205.       _setcursortype( _NOCURSOR );
  206.       printf( "\n\n\n\n\n\n\n\n\n\n\n\n" );
  207.       cprintf( "                             PRESS A KEY TO BEGIN" );
  208.       while ( !getch() );
  209.  
  210.       graphics_setup( WHITE );
  211.  
  212.       settextjustify( CENTER_TEXT, CENTER_TEXT );
  213.       settextstyle( GOTHIC_FONT, HORIZ_DIR, 4 );
  214.       setcolor( LIGHTMAGENTA );
  215.       outtextxy( TOPX, TOPY - 30, title_msg );  /*******************/
  216.  
  217.       settextstyle( TRIPLEX_FONT, HORIZ_DIR, 1 );
  218.       setcolor( LIGHTGREEN );
  219.       outtextxy( TOPX, TOPY + 20, esc_msg );
  220.  
  221.  
  222.       t1.initialize_clock();
  223.       t1.moves++;
  224.       t1.display_moves();
  225.       t1.clock_on();
  226.  
  227.       while ( PLAY )  //Forever, until keypress
  228.      {
  229.      t1.moves++;  
  230.      t1.display_moves();
  231.      t1.clock_on();
  232.      }
  233.  
  234. } // End play()
  235.  
  236.  
  237. void opening_screen()
  238. {
  239.    char topline[] = "Countdown Timer",
  240.     by_line[] = "by",
  241.     name_line[] = "M\\Cooper",
  242.     endline[] = "PRESS A KEY";
  243.  
  244.       graphics_setup( LIGHTCYAN );
  245.       settextstyle( GOTHIC_FONT, HORIZ_DIR, HEADLINE_SIZE );
  246.       settextjustify( CENTER_TEXT, CENTER_TEXT );
  247.       setcolor( LIGHTRED );
  248.       outtextxy( TOPX, TOPY, topline );
  249.  
  250.       settextstyle( TRIPLEX_FONT, HORIZ_DIR, BY_LINE_SIZE );
  251.       setcolor( BLUE );
  252.       outtextxy( BY_LINE_X, BY_LINE_Y, by_line );
  253.  
  254.       setfillstyle( BAR_PATTERN, BAR_COLOR );
  255.       bar3d( BAR_LEFT, BAR_TOP, BAR_RIGHT, BAR_BOTTOM, BAR_DEPTH, BAR_TOPFLAG );
  256.  
  257.       setfillstyle( PIE_PATTERN, PIE_COLOR );
  258.       pieslice( PIE1_X, PIE_Y, PIE_STARTANGLE, PIE_ENDANGLE, PIE_RADIUS );
  259. //    pieslice( PIE2_X, PIE_Y, PIE_STARTANGLE, PIE_ENDANGLE, PIE_RADIUS );
  260.       circle( PIE1_X, PIE_Y, CIRC_RAD );
  261. //    circle( PIE2_X, PIE_Y, CIRC_RAD );
  262.       setlinestyle( SOLID_LINE, 0xFFFF, THICK_WIDTH );
  263.       setcolor( LIGHTRED );
  264.       line( LINE1_X, LINE_Y1, LINE1_X, LINE_Y2 );
  265. //    line( LINE2_X, LINE_Y1, LINE2_X, LINE2_Y2 );
  266.       setfillstyle( PIE_PATTERN, WHITE );
  267.       bar( B1_LEFT, B1_TOP, B1_RIGHT, B_BOTTOM );
  268. //    bar( B2_LEFT, B2_TOP, B2_RIGHT, B_BOTTOM );
  269.  
  270.  
  271.       settextstyle( TRIPLEX_FONT, HORIZ_DIR, NAME_LINE_SIZE );
  272.       setcolor( BLUE );
  273.       outtextxy( NAME_LINE_X, NAME_LINE_Y, name_line );
  274.  
  275.       sleep( DELAY );
  276.  
  277.       settextstyle( TRIPLEX_FONT, HORIZ_DIR, ENDLINE_SIZE );
  278.       setcolor( RED );
  279.       outtextxy( ENDLINE_X, ENDLINE_Y, endline );
  280.  
  281.       getch();
  282.       closegraph();
  283.  
  284.       return;
  285. }
  286.  
  287.  
  288. void CountdownTimer::erase()
  289. {
  290.       display_time();
  291.       setcolor( WHITE );
  292.       outtextxy( BLK_TIME, Y_TIMEPOS, line_clear ); /* Erase time display */
  293.  
  294. }
  295.  
  296. void CountdownTimer::reset_timer()
  297. {
  298.       settextstyle( TRIPLEX_FONT, HORIZ_DIR, 5 );
  299.       erase();
  300.       convert ( total_seconds_mem ); /***RESET TIME*****/
  301.       initialize_clock();
  302.  
  303.       return;
  304.  
  305. }